package com.aduerr.v1shouyin.view;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.aduerr.v1shouyin.R;


/**
 * 成功或者失败dailog  重用的自定义控件
 */
public class BandOrUnbandGuidResultDialog extends Dialog {

    /**
     * 上下文对象 *
     */
    Activity context;
    int showtype;
    String content;
    private View.OnClickListener cancleClickListener;
    private ImageView cancle;
    private ImageView incon;
    private TextView success;
    private TextView tv_money;
    private TextView time;
    private int recLen = 3;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch(msg.what) {
                case 1:
                    recLen--;
                    time.setText("" + recLen);
                    if(recLen > 0) {
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);      // send message
                    } else {
                        dismiss();
                    }
            }
            return false;
        }
    });

    public BandOrUnbandGuidResultDialog(Activity context) {
        super(context);
        this.context = context;
    }

    public BandOrUnbandGuidResultDialog(Activity context, int theme,
                                        View.OnClickListener cancleClickListener,
                                        int showtype) {
        super(context, theme);
        this.context = context;
        this.cancleClickListener = cancleClickListener;
        this.showtype = showtype;//判断是收款成功还是绑定成功
        this.content = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定布局
        this.setContentView(R.layout.guid_result_dialog);

        /*
         * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
         * 对象,这样这可以以同样的方式改变这个Activity的属性.
         */
        Window dialogWindow = this.getWindow();
        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//         p.height = (int) (d.getHeight() * 0.3); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(p);

        // 根据id在布局中找到控件对象
        cancle = (ImageView) this.findViewById(R.id.iv_cancle);
        incon = (ImageView) this.findViewById(R.id.iv_dailog_icon);
        success = (TextView) this.findViewById(R.id.tv_dailog_sucess);
        tv_money = (TextView) this.findViewById(R.id.tv_monney);
        time = (TextView) this.findViewById(R.id.tv_dailog_time);
        initUI();

        // 为按钮绑定点击事件监听器
        cancle.setOnClickListener(cancleClickListener);
        this.setCancelable(true);
        Message message = handler.obtainMessage(1);     // Message
        handler.sendMessageDelayed(message, 1000);
    }

    private void initUI() {
        switch(showtype) {
            case 99://解绑成功的界面
                tv_money.setVisibility(View.GONE);
                success.setText("解绑成功");
                incon.setBackgroundResource(R.drawable.img_dailog_sucuss);
                break;
            case 100://绑定成功的界面
                tv_money.setVisibility(View.GONE);
                success.setText("绑定成功");
                incon.setBackgroundResource(R.drawable.img_dailog_sucuss);
                break;
            case 101://绑定失败的界面
                tv_money.setVisibility(View.GONE);
                success.setText("Guid绑定失败,请重新绑定");
                incon.setBackgroundResource(R.drawable.img_dailog_fail);
                break;
            case 102://收款成功的界面
                success.setText("收款成功");
                incon.setBackgroundResource(R.drawable.img_dailog_fail);
                break;
            case 103://收款失败的界面
                success.setText("收款失败");
                incon.setBackgroundResource(R.drawable.img_dailog_fail);
                break;
            case 104://收款失败的界面
                success.setText("退款成功");
                incon.setBackgroundResource(R.drawable.img_dailog_sucuss);
                break;
            case 105://收款失败的界面
                success.setText("退款失败");
                incon.setBackgroundResource(R.drawable.img_dailog_fail);
                break;
            case 106://收款失败的界面
                success.setText("核销成功");
                incon.setBackgroundResource(R.drawable.img_dailog_sucuss);
                break;
            case 107://收款失败的界面
                success.setText("核销失败");
                incon.setBackgroundResource(R.drawable.img_dailog_fail);
                break;
        }
    }

    public void setMoney(String money) {
        tv_money.setText(money);
    }

}
